<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:14
 */
class Check extends CI_Controller {
    public function __construct(){
        $this->CI = &get_instance();//获取CI对象
        $this->CI->load->model('user_token_model');
    }

    public function check(){
        if(!preg_match("/login*/",uri_string())){
            $this->CI->load->helper('url');
            // $data = $this->CI->input->post();
            // $u = $data['user'];
            //$u = json_decode($u,true);
            /* $data=$_POST['user'];
              $data = json_decode($data,true);
              $id=data['user_id'];*/
            $ip=$_SERVER["REMOTE_ADDR"];
            $user=$this->CI->user_token_model->findByIp($ip);
            if(isset($user)){//如果有人占用这个ip
                if(strtotime($user['expire_time'])>strtotime(date('Y-m-d H:i:s'))){//如果没过期
                    $data=array(
                        'user_id'=>$user['user_id'],
                        'update_time'=>date("Y-m-d H:i:s"),
                        'expire_time'=>date("Y-m-d H:i:s",strtotime("+1 weeks")),
                        'ip'=>$_SERVER["REMOTE_ADDR"]
                    );
                    $this->CI->user_token_model->update($data);
                }else{//ip还在过期了
                    $data=array(
                        'user_id'=>$user['user_id'],
                        'ip'=>0
                    );
                    $this->CI->user_token_model->update($data);
                    redirect('login/login/login');
                    return;
                }
            }else{
                redirect('login/login/login');
                return;
            }
        }
    }
}